package com.ebupt.migu.music.api.util;

import com.ebupt.migu.music.api.entity.StrategyInfoEntity;
import com.ebupt.migu.music.api.mapper.MemberPopMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

@Service
public class CrowdServiceImpl implements  CrowdService{

    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private MemberPopMapper memberPopMapper;

    /**
     *  判断某个redis 缓存中是否存在
     * @param key
     * @param userId
     * @return
     */
    private Boolean isHave(String key,String userId) {
            Set set = redisTemplate.opsForSet().members(key);
            List<String> list = new ArrayList<>(set);
            return list.stream().filter(m -> m.equals(userId)).findAny().isPresent();
    }

    /**
     *  根据登录状态 获取缓存的key
     * @param userStatus
     * @param groupId
     * @return
     */
    private String getkey(String userStatus,String groupId){
        String  redis_key=null;
        switch (userStatus){
            case "1":
                redis_key=groupId.concat("-phone");
                break;
            case "2":
                redis_key=groupId.concat("-uid");
                break;
            case "3":
                redis_key=groupId.concat("-imei");
                break;
        }
        return redis_key;
    }

    /**
     *  传递过来的参数 首先
     * @param strategyInfoEntity
     */
    private Boolean  strategyInfoHave(StrategyInfoEntity strategyInfoEntity,String userId,String  userStatus){
        //  判断黑名单有没有 如果有直接返回false
        String black=strategyInfoEntity.getBlack();
        String white=strategyInfoEntity.getWhite();
        //  是否存在于黑名单
        Boolean  isHaveInBlack=false;
        Boolean  isHaveInWhite=false;
        if (StringUtils.isNotEmpty(black)){
            isHaveInBlack=isHaveIn(black,userId,userStatus);
        }
        if (isHaveInBlack){
            // 存在于黑名单时 直接返回
            return false;
        }else {
            //  在此说明黑名单不存在，那么判断白名单有没有 有直接返回true
            if (StringUtils.isNotEmpty(white)){
               isHaveInWhite=isHaveIn(white,userId,userStatus);
            }
            if (isHaveInWhite){
                // 存在于白名单时 直接返回true
                return true;
            }
            // 否则去redis中查询是否有 返回boolean
            String key=getkey(userStatus,strategyInfoEntity.getGroupId());
            // 判断key是否存在
            if (redisTemplate.hasKey(key)){
                return  isHave(key,userId);
            }
            return  false;
        }

    }


    /**
     *  传递过来的参数 首先
     * @param strategyInfoList
     */
    @Override
    public  StrategyInfoEntity  queryResult(List<StrategyInfoEntity> strategyInfoList,String userId,String  userStatus){
        Boolean ishave;
        StrategyInfoEntity  strategyInfoEntitys=null;
        for (StrategyInfoEntity strategyInfoEntity:strategyInfoList) {
            if (StringUtils.isNotBlank(strategyInfoEntity.getGroupId())) {
                ishave = strategyInfoHave(strategyInfoEntity, userId, userStatus);
                if (ishave) {
                    strategyInfoEntitys = strategyInfoEntity;
                    break;
                }
            }
        }
        return strategyInfoEntitys;
    }

    /**
     *   判断这个人是否存在于白名单或者黑名单
     * @param white/black
     * @param userId
     * @return
     */
    private  Boolean  isHaveIn(String white,String userId,String  userStatus){
            //  根据白名单去数据库查询对应的白名单id集合
            Map<String,Object>  map=new HashMap<>();
            List<String> list= Arrays.asList(white .split(",")).stream().map(s -> (s.trim())).collect(Collectors.toList());
            map.put("list",list);
            int  type=0;
            switch (userStatus){
                case "1" :
                    // 查询手机号
                    type=1;
                    break;
                case "2" :
                    // uid
                    type=2;
                    break;
                case "3" :
                    // idfa/imei
                    type=3;
                    break;

            }
            map.put("type",type);
            List<String> lists=memberPopMapper.rosterInfoByType(map);
            //  白名单存在为true  不存在为false
            return lists.stream().filter(m -> m.equals(userId)).findAny().isPresent();
    }


}
